# coding=utf-8
import numpy as np
import imutils
import cv2


# 定义简单的图片检测函数，这个思路就是单张图片的检测思路
def detect(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
    gradX = cv2.Sobel(gray, ddepth=ddepth, dx=1, dy=0, ksize=-1)
    gradY = cv2.Sobel(gray, ddepth=ddepth, dx=0, dy=1, ksize=-1)

    gradient = cv2.subtract(gradX, gradY)
    gradient = cv2.convertScaleAbs(gradient)
    cv2.imwrite("test2.jpg", gradient)

    blurred = cv2.blur(gradient, (9, 9))
    (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)
    cv2.imwrite("test3.jpg", thresh)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    cv2.imwrite("test3.jpg", closed)

    closed = cv2.erode(closed, None, iterations=4)
    closed = cv2.dilate(closed, None, iterations=4)
    cv2.imwrite("test4.jpg", closed)

    contour = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)
    contour = imutils.grab_contours(contour)

    if len(contour) == 0:
        return None

    c = sorted(contour, key=cv2.contourArea, reverse=True)[0]
    rect = cv2.minAreaRect(c)
    box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
    box = np.int0(box)

    return box
# reference: https://www.pyimagesearch.com/2014/12/15/real-time-barcode-detection-video-python-opencv/


if __name__ == '__main__':
    # capture = cv2.VideoCapture(0)
    #
    # # 循环处理每一帧
    # while True:
    #     # 获取当前帧
    #     ret, frame = capture.read()

        frame = cv2.imread('test1.jpg')

        # 进行单张图片检测
        box = detect(frame)

        # 如果发现了条形码则绘制结果
        if box is not None:
            cv2.drawContours(frame, [box], -1, (0, 255, 0), 2)
            cv2.imwrite("result.jpg", frame)

    #     # 显示当前帧的结果
    #     cv2.imshow("Frame", frame)
    #     key = cv2.waitKey(1) & 0xFF
    #
    #     # 当用户按下q键时退出整个循环
    #     if key == ord("q"):
    #         break
    #
    # capture.release()
    #
    # # 关闭所有的窗口
    # cv2.destroyAllWindows()
